/**
 * 
 */
package com.hfjyz.registration.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.registration.command.AnnouncementListCommand;
import com.hfjyz.registration.command.DepartEditCommand;
import com.hfjyz.registration.command.DepartListCommand;
import com.hfjyz.registration.dao.DepartDao;
import com.hfjyz.registration.dao.ExamDao;
import com.hfjyz.registration.model.Depart;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.util.MasterUtil;

/**
 *
 */
public class DepartServiceImpl extends AbstractServiceImpl implements
		DepartService {
	private DepartDao departDao;
	private ExamDao examDao;

	@SuppressWarnings("rawtypes")
	@Override
	public Map initEdit(DepartEditCommand cmd, Errors errors) throws Exception {
		if (cmd != null && cmd.getId() != null) {
			Depart depart = departDao.get(cmd.getId());
			if (depart != null) {
				cmd.setId(depart.getId());
				cmd.setName(depart.getName());
				cmd.setDescription(depart.getDescription());
				cmd.setExamId(depart.getExamId());
			}
		}
		AnnouncementListCommand alc = new AnnouncementListCommand();
		alc.setStatus(MasterUtil.getInstance().getMaster(10, "07").getId());
		List<Exam> exams = examDao.search(alc);
		cmd.setExams(exams);
		return null;
	}

	@Transactional
	@Override
	public void editOk(HttpServletRequest request, DepartEditCommand cmd,
			BindException errors) throws Exception {

		Depart depart = new Depart();
		depart.setId(cmd.getId());
		depart.setName(cmd.getName());
		depart.setDescription(cmd.getDescription());
		depart.setExamId(cmd.getExamId());
		if (depart.getId() == null) {
			departDao.add(depart);
		} else {
			departDao.update(depart);
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map initSearch(DepartListCommand cmd, Errors errors)
			throws Exception {
		List<Exam> exams = examDao.listAll();
		cmd.setExams(exams);
		if (cmd.getExamId() == null && exams != null && !exams.isEmpty()) {
			cmd.setExamId(exams.get(0).getId());
		}
		List<Depart> departs = departDao.search(cmd);
		cmd.setResult(departs);
		return null;
	}

	@Override
	public void search(HttpServletRequest request,
			HttpServletResponse response, DepartListCommand cmd,
			BindException errors) throws Exception {
		List<Depart> departs = departDao.search(cmd);
		cmd.setResult(departs);
	}

	@Transactional
	@Override
	public void delete(HttpServletRequest request, DepartListCommand cmd,
			BindException errors) throws Exception {
		if (cmd.getDid() != null) {
			departDao.delete(departDao.get(cmd.getDid()));
		}
	}

	/**
	 * @return the departDao
	 */
	public DepartDao getDepartDao() {
		return departDao;
	}

	/**
	 * @param departDao
	 *            the departDao to set
	 */
	public void setDepartDao(DepartDao departDao) {
		this.departDao = departDao;
	}

	/**
	 * @return the examDao
	 */
	public ExamDao getExamDao() {
		return examDao;
	}

	/**
	 * @param examDao
	 *            the examDao to set
	 */
	public void setExamDao(ExamDao examDao) {
		this.examDao = examDao;
	}

}
